package cn.fl.system.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.service.ISysFlowManageOrgRService;
import cn.fl.system.vo.SysFlowManageOrgRVO;
import fl.flow.facade.flow.condition.SysFlowManageOrgRQueryCondition;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @Description: 流程配置管理与组织机构关系 控制层
 */
@Controller
@RequestMapping("/system/sysflowmanageorgr")
public class SysFlowManageOrgRController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(SysFlowManageOrgRController.class);

	/**
	 * SysFlowManageOrgR 服务层BEAN
	 */
	@Autowired
	private ISysFlowManageOrgRService sysFlowManageOrgRService;

	/**earch
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		String manageId = request.getParameter("manageId");
		String procDefKey = request.getParameter("procDefKey");
		ModelAndView view = new ModelAndView("flow/sysflowmanageorgr/flowManageOrgRTree");
		view.addObject("manageId", manageId);
		view.addObject("procDefKey", procDefKey);
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String orgCd = request.getParameter("orgCd");
		String flowNm = request.getParameter("flowNm");//流程名称
		String modelTyp = request.getParameter("modelTyp");//业务类型
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(currentPage)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		SysFlowManageOrgRQueryCondition qc = new SysFlowManageOrgRQueryCondition();
		if(StringUtils.isNotEmpty(flowNm)){
			qc.setFlowNm(flowNm);
		}

		if(StringUtils.isNotEmpty(modelTyp)){
			qc.setModelTyp(modelTyp);
		}
		//qc.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
		qc.setOrgCd(orgCd);
		qc.setCurrOrgCd(CurrentThreadContext.getCurrentOrgCd());
		qc.setSysCd(CurrentThreadContext.getCurrentSysCd());
		page.setCondition(qc);

		Pager pager = this.sysFlowManageOrgRService.searchListPage(page);
		List<SysFlowManageOrgRVO> list = (List<SysFlowManageOrgRVO>) pager.getResultList();
		ListPageVO<SysFlowManageOrgRVO> listPage = new ListPageVO<SysFlowManageOrgRVO>(list, pager.getTotalCount());
		return listPage;
	}
	
	/**
	 * 根据流程主键获取组织架构树以及把已经选中的组织同时显示出来
	 */
	@RequestMapping(value = "/initOrgTree.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initOrgTree(HttpServletRequest request, HttpServletResponse response) {
		String manageId = request.getParameter("manageId");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES,"获取成功!");
		if(StringUtils.isEmpty(manageId)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("流程管理主键不能为空！");
			return result;
		}
		JSONObject tree = new JSONObject();
		try {
			tree = this.sysFlowManageOrgRService.searchOrgWithFlowRTree(Long.valueOf(manageId));
			if(null == tree){
				tree = new JSONObject();
			}
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error(ex.getMessage(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("初始化失败!");
			logger.error("初始化失败!", ex);
		}
		result.setData(tree);
		return result;
	}
	
	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String data = request.getParameter("data");//数组
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES,"操作成功!");
		if(StringUtils.isEmpty(data)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("参数不完整！");
			return result;
		}
		
		try {
			List<SysFlowManageOrgRVO> list = new ArrayList<>();
			SysFlowManageOrgRVO entity = null;
			JSONArray dataArray = JSONArray.fromObject(data);
			if(dataArray.isArray()){
				String buOrgCd = CurrentThreadContext.getCurrentBuOrgCd();
				Long usrId = CurrentThreadContext.getCurrentUserId();
				String sysCd = CurrentThreadContext.getCurrentSysCd();
				int isDel = BaseConstant.IS_YESNO_NO;
				Boolean pass = true;//参数空值控制
				for (Object object : dataArray) {
					 JSONObject obj = (JSONObject)object;
					 String manageId = obj.optString("id");
					 String procDefKey = obj.optString("procDefKey");
					 String modelTyp = obj.optString("modelTyp");
					 String orgCd = obj.optString("orgCd");
					 
					 if(StringUtils.isEmpty(manageId) || 
							 StringUtils.isEmpty(procDefKey) || 
							 StringUtils.isEmpty(modelTyp) || 
							 StringUtils.isEmpty(orgCd)){
						 pass = false;
						 break;
					 }
					
					 entity = new SysFlowManageOrgRVO();
					 entity.setFlowManageId(Long.valueOf(manageId));
					 entity.setProcDefKey(procDefKey);
					 entity.setModelTyp(modelTyp);
					 entity.setIsDel(isDel);
					 entity.setOrgCd(orgCd);
					 entity.setBuOrgCd(buOrgCd);
					 entity.setSysCd(sysCd);
					 entity.setCrtTm(new Date());
					 entity.setCrtUsrId(usrId);
					 entity.setMdfTm(new Date());
					 entity.setMdfUsrId(usrId);
					 list.add(entity);
				}
				
				if(!pass){
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo("参数不完整！");
					return result;
				}
			}
			
			boolean success = this.sysFlowManageOrgRService.modify(list);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("操作失败！");
			}
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("操作失败!");
		}
		return result;
	}
}

